/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.impl;

import dao.PetDao;
import entity.Pet;
import entity.PetOwner;
;
import until.BaseDAO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;import until.BaseDAO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Administrator
 */


public class PetMYSQLImpl extends BaseDAO implements PetDao {

    /**
     *
     * @param pet
     * @return
     */
    @Override
    public int Add(Pet pet) {
        int r = 0;
        try {
            Connection con;
            con = BaseDAO.getConnection();
            PreparedStatement pst = con.prepareStatement("insert into pet(NAME,TYPENME,HEALTH,LOVE,BIRTHDAY,OWNER_ID,STORE_ID,PRICE)VALUE(?,?,?,?,?,?,?,?)");
            pst.setString(1, pet.getName());
            pst.setString(2, pet.getTypename());
            pst.setInt(3, pet.getHealth());
            pst.setInt(4, pet.getLove());
            pst.setDate(5, (Date) pet.getBirthday());
            pst.setInt(6, pet.getOwner_id());
            pst.setInt(7, pet.getStore_id());
            pst.setInt(8, pet.getPrice());
            r = pst.executeUpdate();
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }
        return r;
    }

    @Override
    public Pet getId(int id) {
        Pet pet = null;
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from pet where ID=" + id);

            if (rs.next()) {
                pet = new Pet();
                pet.setName(rs.getString(1));
                pet.setTypename(rs.getString(2));
                pet.setHealth(rs.getInt(3));
                pet.setLove(rs.getInt(4));
                pet.setBirthday(rs.getDate(5));
                pet.setOwner_id(rs.getInt(6));
                pet.setStore_id(rs.getInt(7));
                pet.setPrice(rs.getInt(8));
            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return pet;
    }

    @Override
    public List<Pet> getPets() {
        List<Pet> ls = new ArrayList<>();
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from pet");

            while (rs.next()) {
                Pet pet = new Pet();
                pet.setId(rs.getInt("ID"));
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setOwner_id(rs.getInt("OWNER_ID"));
                pet.setStore_id(rs.getInt("STORE_ID"));
                pet.setPrice(rs.getInt("PRICE"));
                ls.add(pet);
            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ls;
    }

    @Override
    public List<Pet> getRemainPets() {
        List<Pet> ls = new ArrayList<>();
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from pet where OWNER_ID is NULL");

            while (rs.next()) {
                Pet pet = new Pet();
                pet.setId(rs.getInt("ID"));
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setOwner_id(rs.getInt("OWNER_ID"));
                pet.setStore_id(rs.getInt("STORE_ID"));
                pet.setPrice(rs.getInt("PRICE"));
                ls.add(pet);
            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ls;
    }

    @Override
    public List<Pet> getPetsOwner(int ownerid) {
        List<Pet> ls = new ArrayList<>();
        try {
            Connection con = BaseDAO.getConnection();
            ResultSet rs = con.createStatement().executeQuery("select * from pet where OWNER_ID ='" + ownerid + "'");

            while (rs.next()) {
                Pet pet = new Pet();
                pet.setId(rs.getInt("ID"));
                pet.setName(rs.getString("NAME"));
                pet.setTypename(rs.getString("TYPENAME"));
                pet.setHealth(rs.getInt("HEALTH"));
                pet.setLove(rs.getInt("LOVE"));
                pet.setBirthday(rs.getDate("BIRTHDAY"));
                pet.setOwner_id(rs.getInt("OWNER_ID"));
                pet.setStore_id(rs.getInt("STORE_ID"));
                pet.setPrice(rs.getInt("PRICE"));
                ls.add(pet);
            }

            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return ls;

    }

    @Override
    public int update(Pet pet, PetOwner po) {
        int state = 0;
        try {
            Connection con = BaseDAO.getConnection();
            con.createStatement().execute("update pet set  OWNER_ID=" + po.getId() + ",STORE_ID =NULL where ID=" + pet.getId());
            state = 1;
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return state;
    }

    @Override
    public int updateSell(Pet pet, int petStoreId) {
        int state = 0;
        try {
            Connection con = BaseDAO.getConnection();
            con.createStatement().execute("update pet set  STORE_ID=" + petStoreId + ",OWNER_ID =NULL where ID=" + pet.getId());
            state = 1;
            BaseDAO.closeConnection(con);
        } catch (SQLException ex) {
            Logger.getLogger(PetMYSQLImpl.class.getName()).log(Level.SEVERE, null, ex);
        }

        return state;
    }

}
